
/***********************************************************************
 * Clock class
 * 
 * Implements a clock that notifies its observers at every tick
 *
 * Signature:
 *  public static Clock create (int initTime);
 *  public void registerObserver (Observer<Integer> obs);
 *  public void tick ();
 ***********************************************************************/

public class Clock {

  private int time;
  private List<Observer<Integer>> observers;

  private Clock (int c) {
    time = c;
    observers = List.empty();
  }

  public static Clock create (int c) {
    return new Clock(c);
  }

  public void registerObserver (Observer<Integer> ob) {
    observers = List.cons(ob,observers);
  }
  
  public int currentTime() {
    return time;
  }
  
  public void tick () {
    time++;
    for (Observer<Integer> obs : observers)
      obs.notify(time);
  }

}